﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Lyh.Libs.MyConverter;
using Lyh.Libs.WebUtility;

namespace Lyh.API.Feixin
{
    public class WebMessenger
    {
        private HttpClient httpClient=new HttpClient();

        /// <summary>
        /// 获取验证码图片
        /// </summary>
        /// <returns></returns>
        public Image GetPic()
        {
            var url = string.Format("https://webim.feixin.10086.cn/WebIM/GetPicCode.aspx?Type=ccpsession&{0}", new Random().NextDouble());
            var parameter = new HttpParameter()
            {
                Url = url,
                Method = HttpMethod.GET
            };
            var stream = httpClient.GetImageStream(parameter);
            return Image.FromStream(stream);
        }

        /// <summary>
        /// 登录网页版飞信
        /// </summary>
        /// <param name="userName">手机号、飞信号或邮箱</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public Result Login(string userName, string password, string code)
        {
            var dic = new Dictionary<string, string>
            {
                {"UserName", userName},
                {"Pwd", password},
                {"OnlineStatus", "400"},
                {"AccountType", "1"},
                {"Ccp", code}
            };

            var parameter = new HttpParameter
            {
                Url = "https://webim.feixin.10086.cn/WebIM/Login.aspx",
                Host = "webim.feixin.10086.cn",
                Origin = "https://webim.feixin.10086.cn",
                Referer = "https://webim.feixin.10086.cn/loginform.aspx",
                Method = HttpMethod.POST,
                Data = dic
            };

            var json = httpClient.Request(parameter);
            var result = JsonConverter.FromJson<Result>(json);
            return result;
        }

        /// <summary>
        /// 获取个人资料
        /// </summary>
        /// <returns></returns>
        public PersonalInfo GetPersonalInfo()
        {
            var url = string.Format("https://webim.feixin.10086.cn/WebIM/GetPersonalInfo.aspx?Version=0&ssid={0}", GetSsid());
            var parameter = new HttpParameter()
            {
                Url=url,
                Referer = "https://webim.feixin.10086.cn/main.aspx",
                Method =HttpMethod.GET
            };

            var json = httpClient.Request(parameter);
            var info = JsonConverter.FromJson<PersonalInfo>(json);
            return info;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="from">发送人的uid</param>
        /// <param name="to">接收人的uid(多个以“,”隔开）</param>
        /// <param name="msg">短信内容</param>
        /// <returns></returns>
        public Result Send(string from,string to,string msg)
        {
            var dic = new Dictionary<string, string>
            {
                {"UserName", from},
                {"Message",Uri.EscapeDataString(msg)},
                {"Receivers", to},
                {"ssid", GetSsid()}
            };

            var parameter = new HttpParameter()
            {
                Url = "https://webim.feixin.10086.cn/content/WebIM/SendSMS.aspx?Version=160",
                Host = "webim.feixin.10086.cn",
                Origin = "https://webim.feixin.10086.cn",
                Referer = "https://webim.feixin.10086.cn/content/freeSms.htm?tabIndex=0",
                Method = HttpMethod.POST,
                Data = dic
            };

            var json = httpClient.Request(parameter);
            var result = JsonConverter.FromJson<Result>(json);
            return result;
        }

        /// <summary>
        /// 获得联系人信息
        /// </summary>
        /// <returns></returns>
        public ConnectInfo GetConnectInfo()
        {
            var url = string.Format("https://webim.feixin.10086.cn/WebIM/GetConnect.aspx?Version=1&ssid={0}", GetSsid());
            var parameter = new HttpParameter()
            {
                Url = url,
                Referer = "https://webim.feixin.10086.cn/main.aspx",
                Method = HttpMethod.GET
            };

            var json = httpClient.Request(parameter);
            var info = JsonConverter.FromJson<ConnectInfo>(json);
            return info;
        }

        /// <summary>
        /// 获得会话编号
        /// </summary>
        /// <returns></returns>
        private string GetSsid()
        {
            var lst=httpClient.GetAllCookies();
            var cookie = lst.Single(c => c.Name == "webim_sessionid");
            return cookie.Value;
        }

    }
}
